/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.pp.bizon.yandex.nb;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.swing.tree.TreeNode;

import ua.pp.bizon.yandex.api.impl.EventSupport;
import ua.pp.bizon.yandex.nb.model.LocalNode;
import ua.pp.bizon.yandex.nb.model.LocalPhoto;
import ua.pp.bizon.yandex.nb.model.RemoteAlbumNode;
import ua.pp.bizon.yandex.nb.model.RemoteNode;
import ua.pp.bizon.yandex.nb.model.RemotePhoto;
import ua.pp.bizon.yandex.uploader.atom.Photo;
import ua.pp.bizon.yandex.uploader.atom.PhotosList;

/**
 * 
 * @author Bizon
 */
public class PhotosNbModel extends EventSupport{

	public static final class JpgFilter implements FileFilter {
		@Override
		public boolean accept(File paramFile) {
			return paramFile.getName().substring(paramFile.getName().length() -4).equalsIgnoreCase(".jpg");
		}
	}

	private final class NullTreeNode implements TreeNode {
		@Override
		public TreeNode getChildAt(int paramInt) {
			return null;
		}

		@Override
		public int getChildCount() {
			return 0;
		}

		@Override
		public TreeNode getParent() {
			return null;
		}

		@Override
		public int getIndex(TreeNode paramTreeNode) {
			return -1;
		}

		@Override
		public boolean getAllowsChildren() {
			return false;
		}

		@Override
		public boolean isLeaf() {
			return true;
		}

		@Override
		public Enumeration<?> children() {
			return null;
		}
	}

	private String[] drives;
	private RemoteNode leftTree;

	private Map<String, TreeNode> rightTree;

	public String[] getDrives() {
		if (drives == null) {
			init();
		}
		return drives;
	}

	public RemoteNode getLeftTree() {
		if (leftTree == null) {
			init();
		}
		return leftTree;
	}
	
	

	public TreeNode getRightTree(String path) {
		if (rightTree == null) {
			init();
		}
		if (path == null){
			return new NullTreeNode();
		}
		return rightTree.get(path);
	}

	private void init() {
		drives = new String[File.listRoots().length];
		for (int i = 0; i < File.listRoots().length; i++) {
			drives[i] = File.listRoots()[i].toString();
		}
		leftTree = new RemoteNode(); 
		//RemoteNode(AppServiceFactory.get().getAlbumsList());
		//AppServiceFactory.addChangeListener(leftTree);
		rightTree = new TreeMap<String, TreeNode>();
		for (String s : drives) {
			rightTree.put(s, new LocalNode(s));
		}
	}

	
	public LocalPhoto[] getPhotosBySelection(LocalNode localNode) {
		File dir = localNode.getFile();
		File[] pics = dir.listFiles(new JpgFilter());
		LocalPhoto[] res = new LocalPhoto[pics.length];
		for (int i =0; i < pics.length; i++){
			res[i] = new LocalPhoto(pics[i]);
		}
		return res;
	}
	
	public RemotePhoto[] getPhotosBySelection(RemoteAlbumNode selection){
		PhotosList photosList = selection.getAlbum().getList();
		List<RemotePhoto> photos = new ArrayList<RemotePhoto>(photosList.size());
		for (Photo p: photosList){
			photos.add(new RemotePhoto(p));
		}
		return photos.toArray(new RemotePhoto[0]);
	}
}
